# Base image for building the application with Gradle and JDK 17
FROM gradle:8.10.2-jdk17 AS build

# 设置 GRADLE_USER_HOME 环境变量
ENV GRADLE_USER_HOME=/gradle
COPY gradle-cache /gradle

# Set the working directory
WORKDIR /app

# Copy the Gradle wrapper files if using the wrapper
COPY gradlew gradlew
#COPY gradle gradle

# Copy other build configuration files
COPY build.gradle settings.gradle ./

# Copy the source code for all modules
COPY antlr ./antlr
COPY api ./api
COPY conf ./conf
COPY core ./core
COPY engine ./engine
COPY evaluate ./evaluate
COPY plugins/dameng ./plugins/dameng
COPY plugins/kingbase ./plugins/kingbase
COPY plugins/mysql ./plugins/mysql
COPY plugins/oracle ./plugins/oracle
COPY plugins/sqlserver ./plugins/sqlserver
COPY plugins/gbase ./plugins/gbase
COPY plugins/shentong ./plugins/shentong

# Build the application (skip tests for quicker builds)
RUN gradle build -x test --no-daemon --info
RUN gradle :antlr:copyLibs --no-daemon --info
RUN gradle :api:copyLibs --no-daemon --info
RUN gradle :core:copyLibs --no-daemon --info
RUN gradle :engine:copyLibs --no-daemon --info
RUN gradle :evaluate:copyLibs --no-daemon --info
RUN gradle :plugins:dameng:copyLibs --no-daemon --info
RUN gradle :plugins:kingbase:copyLibs --no-daemon --info
RUN gradle :plugins:mysql:copyLibs --no-daemon --info
RUN gradle :plugins:oracle:copyLibs --no-daemon --info
RUN gradle :plugins:sqlserver:copyLibs --no-daemon --info
RUN gradle :plugins:gbase:copyLibs --no-daemon --info
RUN gradle :plugins:shentong:copyLibs --no-daemon --info

# Use a minimal JRE for the runtime
FROM openjdk:17-jdk-slim AS runtime

# for health check
RUN apt-get -o Acquire::http::Proxy="http://192.168.0.60:3128/" update && apt-get -o Acquire::http::Proxy="http://192.168.0.60:3128/" install -y curl && rm -rf /var/lib/apt/lists/*

# Set the working directory for the runtime image
WORKDIR /app

# Copy the built JAR file(s) from the build stage
# Assuming module-c produces an executable JAR
#COPY --from=build /app/engine/build/libs/*.jar ./app.jar
COPY --from=build /app/libs/*.jar ./
# Copy the configuration files to the runtime image
COPY --from=build /app/conf ./conf

COPY jdbc/*jar ./

# Expose the desired application port
EXPOSE 9588

# Command to run the application
#CMD ["java", "-jar", "app.jar"]
CMD ["java", "-cp", "/app/conf:/app/*", "belf.migrate.engine.BelfEngine"]